package admin

import (
	"github.com/astaxie/beego"
	"log"
	"qinnailin.cn/myblog/models"
	"time"
)

type AdminController struct {
	AdminBaseController
}

//后台管理首页
func (this *AdminController) Index() {
	log.Println("admin-controller")
	this.View("index")
}

//编辑文章
func (this *AdminController) ArticleEdit() {
	id := this.GetString("id")
	article := &models.Article{Id: id}
	err := article.Get()
	if err != nil {
		log.Println("文章读取失败：" + err.Error())
		this.Data["Error"] = err.Error()
		this.ViewError()
		return
	}
	this.Data["model"] = article
	this.View("articleedit")
}

//编辑文章
func (this *AdminController) DoArticleEdit() {
	log.Println("测试post")
	var (
		Id    = this.GetString("Id")
		Title = this.GetString("Title")
		Body  = this.GetString("Body")
	)
	//log.Println(Body)
	if Id == "" {
		bean := &models.Article{Id: models.NewObjId(), Title: Title, Summary: beego.Html2str(Body),
			Body: Body, IniTime: time.Now()}
		id := this.Ctx.GetCookie("usertoken")
		bean.Uid = id
		bean.Add()
	} else {
		bean := &models.Article{Id: Id, Title: Title, Body: Body}
		err := bean.Update()
		if err != nil {
			log.Println("更新失败!" + err.Error())
			this.Data["Error"] = err.Error()
			this.ViewError()
			return
		}
	}
	this.Redirect("/", 302)
}
